<template>
    <div class="video">
        <!-- 视频列表 -->
        <div class="video_list">
            <ul>
                <li v-for="(item, index) in videos" :key="index">
                    <div class="header">
                        <div class="play">
                            <i class="video"></i>
                            <span class="count">{{ item.playTime | handleCount }}</span>
                        </div>
                        <img :src="item.coverUrl" alt="" @click="play(item)">
                        <div class="time">{{ min(item) }}:{{ sec(item) }}</div>
                    </div>
                    <div class="info">
                        <div class="title ellipsis">
                            <i class="mv" v-show="item.type==0?true:false"></i>
                            <a :title="item.title" @click="play(item)" v-html="highlight(item.title)"></a>
                        </div>
                        <div class="name ellipsis">
                            <span v-show="item.type==1?true:false">by</span>
                            <a :title="item1.userName" v-for="(item1, index) in item.creator" :key="index" @click="$router.push(`/artlist/${item1.userId}`)">{{ item1.userName }}<em>/</em></a>
                        </div>
                    </div>
                </li>
            </ul>
        </div>
    </div>
</template>

<script>
import { VideoSearchApi } from "@/request/api";
export default {
    data () {
        return {
            videos: [],
        }
    },
    filters: { 
        handleCount(count){
            if(count >= 10000){
                count = (count - count%1000)/10000 + '万'
            }else if(count >= 1000){
                count = (count - count%100)/1000 + '千'
            }
            return count
        }
    },
    computed: {
        keyword() {
            return this.$route.params.keyword;
        }
    },
    watch: {
        keyword: {
            handler(newValue, oldValue){
                if(newValue !== oldValue){
                    VideoSearchApi(newValue).then((res) => {
                        // console.log(res);
                        this.videos = res.data.result.videos;
                    });
                }
            },
            immediate: true,
        }
    },
    // created() {
    //     VideoSearchApi(this.keyword).then((res) => {
    //         // console.log(res);
    //         this.videos = res.data.result.videos;
    //     });
    // },
    methods: {
        highlight(str) {
            // 正则表达式//中间的字符只是字符串
            // 如果需要动态创建一个正则表达式
            // new RegExp(str, rules) str是正则字符串，rules是匹配模式写在字符串中
            const reg = new RegExp(this.keyword, 'gi')
            const highStr = str.replace(reg, `<span style="color: #0c73c2;">${this.keyword}</span>`)
            return highStr
        },
        min(item) {
          if (parseInt(item.durationms / 60000) < 10)
          return "0" + parseInt(item.durationms / 60000);
          else return parseInt(item.durationms / 60000);
        },
        sec(item) {
          let x = parseInt(item.durationms / 60000);
          let y = parseInt(item.durationms / 1000 - x * 60);
          if (y < 10) return "0" + y;
          else return y;
        },
        play(item) {
            if(item.type == 0 ) {
                this.$router.push(`/mvlist/${item.vid}`)
            }else{
                this.$router.push(`/videolist/${item.vid}`)
            }
        }
    }
}
</script>
 
<style lang = "less" scoped>
@import "../../assets/base.less";
.video {
    .video_list {
        ul {
            flex-wrap: wrap;
            display: flex;
            justify-content: flex-start !important;
        }
        li {
            width: 159px;
            height: 130px;
            padding: 0 31px 40px 0;
            &:nth-of-type(5n) {
                padding-right: 0;
            }
            .header {
                position: relative;
                width: 159px;
                height: 90px;
                &:hover {
                    cursor: pointer;
                    text-decoration: underline;
                }
                .play {
                    position: absolute;
                    top: 0;
                    right: 0;
                    width: 100%;
                    height: 20px;
                    line-height: 20px;
                    display: flex;
                    justify-content: flex-end;
                    align-items: center;
                    background: linear-gradient(to left,#333,transparent);
                    .video {
                        margin-right: 5px;
                        width: 15px;
                        height: 10px;
                        display: inline-block;
                        background: url("@/assets/images/public/icon2.png") no-repeat;
                        background-position: -60px -310px;
                    }
                    .count {
                        padding-right: 5px;
                        font-size: 12px;
                        color: #fff;
                        text-shadow: -2px 1px 1px #333;
                    }
                }
                img {
                    width: 159px;
                    height: 90px;
                }
                .time {
                    position: absolute;
                    bottom: 5px;
                    left: 5px;
                    width: 100%;
                    font-size: 12px;
                    color: #fff;
                    text-shadow: -2px 1px 1px #333;
                }
            }
            .info {
                .title {
                    width: 159px;
                    height: 20px;
                    margin: 5px 0;
                    .mv {
                        width: 26px;
                        height: 16px;
                        display: inline-block;
                        background: url("@/assets/images/public/icon2.png") no-repeat;
                        background-position: -270px -480px;
                        margin: 0 4px 0 0;
                    }
                    a {
                        font-size: 14px;
                        color: #333;
                        line-height: 20px;
                        &:hover {
                            cursor: pointer;
                            text-decoration: underline;
                        }
                    }
                }
                .name {
                    width: 159px;
                    line-height: 18px; 
                    span {
                        color: #333;
                        font-size: 12px;
                        margin-right: 5px;
                    }
                    a {
                        color: #666;
                        font-size: 12px;
                        em {
                            margin: 0 3px;
                        }
                        &:hover {
                            cursor: pointer;
                            text-decoration: underline;
                        }
                        &:last-of-type {
                            em {
                                display: none;
                            }
                        }
                    }
                }
            }
        }
    }
}
</style>